#encoding=utf-8
import os
print os.getcwd()
os.chdir('/data/Projects/web/city_hero_manager')
print os.getcwd()
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "gm_site.settings")
from websys.models import LogOnline,Report_Data
import datetime,time
# def report_task():
#     report_info=Report_Data.objects.count()
#
#     time_now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
#
#     print '====time  now ===',time_now
#     if report_info:
#         time_info = Report_Data.objects.all().order_by('-id')[:1][0].create_time
#         gte_time=time_info
#         server_list = LogOnline.objects.filter(create_time__gte=gte_time,
#                                                create_time__lt=time_now).values('server').distinct()
#         platform_list = LogOnline.objects.filter(create_time__gte=gte_time,
#                                                  create_time__lt=time_now).values('platform').distinct()
#         print time_info,'-<<last_time=====server_list>>>',server_list
#         print '-<<=====platform_list>>>',platform_list
#         for server in server_list:
#             online_count=LogOnline.objects.filter(create_time__gte=gte_time,create_time__lt=time_now,server=server['server']).distinct().values('uid').count()
#             new_info=Report_Data()
#             new_info.online_count=online_count
#             new_info.which_from=server['server']
#             if int(online_count)!=0:
#                 new_info.save()
#             print server['server'],'----------online_cont--->>>',online_count
#             print '===============time_info[0].create_time',gte_time
#         for platform in platform_list:
#             online_count = LogOnline.objects.filter(create_time__gte=gte_time, create_time__lt=time_now,
#                                                     platform=platform['platform']).distinct().values('uid').count()
#
#             new_info = Report_Data()
#             new_info.online_count = online_count
#             new_info.which_from = platform['platform']
#             if int(online_count) != 0:
#                 new_info.save()
#             print platform['platform'],'----------online_cont--->>>', online_count
#             print '===============time_info[0].create_time', gte_time
#         print '----------success----------'
#     else:
#         time_s=datetime.datetime.now().strftime('%Y-%m-%d 00:00:00')
#         server_list = LogOnline.objects.filter(create_time__gte=time_s,
#                                                create_time__lt=time_now).values('server').distinct()
#         platform_list = LogOnline.objects.filter(create_time__gte=time_s,
#                                                  create_time__lt=time_now).values('platform').distinct()
#         for server in server_list:
#             online_count=LogOnline.objects.filter(create_time__gte=time_s,create_time__lt=time_now,server=server['server']).distinct().values('uid').count()
#             new_info=Report_Data()
#             new_info.online_count=online_count
#             new_info.which_from=server['server']
#             new_info.save()
#         for platform in platform_list:
#             online_count = LogOnline.objects.filter(create_time__gte=time_s, create_time__lt=time_now,
#                                                     platform=platform['platform']).distinct().values('uid').count()
#             new_info = Report_Data()
#             new_info.online_count = online_count
#             new_info.which_from = platform['platform']
#             new_info.save()
#         print '----------success----------'


def get_back_data():
    #frist_time=LogOnline.objects.all().order_by('id')[:1][0].create_time
    #frist_time='2017-10-24 00:00:00'
    #end_time='2017-11-30 00:00:00'
    frist_time=str(Report_Data.objects.all().order_by('-create_time')[:1][0].create_time)
    #print frist_time,'-----------------------frist_time----------'
    end_time=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    t_date_start = time.mktime(time.strptime(frist_time, '%Y-%m-%d %H:%M:%S'))
    t_date_end = time.mktime(time.strptime(end_time, '%Y-%m-%d %H:%M:%S'))

    b_date = t_date_start
    while b_date <= t_date_end:
        time_local = time.localtime(b_date)
        dt = time.strftime("%Y-%m-%d %H:%M:%S", time_local)
        dts = datetime.datetime.fromtimestamp(b_date)
        delta = datetime.timedelta(minutes=10)
        #delta = datetime.timedelta(hours=1)
        three_s=datetime.timedelta(seconds=30)
        in_times=dts + three_s
        changed_time = dts + delta

        #report_info = Report_Data.objects.count()
        #if report_info:
        server_list = LogOnline.objects.filter(create_time__gte=dt,
                                              create_time__lt=changed_time).values('server').distinct()
        platform_list = LogOnline.objects.filter(create_time__gte=dt,
                                                create_time__lt=changed_time).values('platform').distinct()
        print dt,'-<<last_time=====server_list>>>',server_list
        print changed_time,'-<<=====platform_list>>>',platform_list
        for server in server_list:
            online_count=LogOnline.objects.filter(create_time__gte=dt,create_time__lt=changed_time,server=server['server']).distinct().values('uid').count()
            if  not Report_Data.objects.filter(create_time__gte=dt,create_time__lt=in_times,which_from=server['server']).exists():
                new_info=Report_Data()
                new_info.online_count=online_count
                new_info.which_from=server['server']
                print dt,'=111111111111111111111111',
                new_info.create_time=dt
                print time.strftime('%Y-%m-%d',time.strptime(dt,"%Y-%m-%d %H:%M:%S")), '=2222222222222222222222222222222',
                new_info.create_date=time.strftime('%Y-%m-%d',time.strptime(dt,"%Y-%m-%d %H:%M:%S"))
                if int(online_count)!=0:
                    new_info.save()
                print '===============success====server_list=============='
                print server['server'],'----------online_cont--->>>',online_count
            else:
                print 'exist--======e=-=-=-e=e-e=-=-=-=-'
        for platform in platform_list:
            online_count = LogOnline.objects.filter(create_time__gte=dt, create_time__lt=changed_time,
                                                   platform=platform['platform']).distinct().values('uid').count()
            if not Report_Data.objects.filter(create_time__gte=dt,create_time__lt=in_times, which_from=platform['platform']).exists():
                new_info = Report_Data()
                new_info.online_count = online_count
                new_info.which_from = platform['platform']
                new_info.create_time = dt
                new_info.create_date = time.strftime('%Y-%m-%d', time.strptime(dt, "%Y-%m-%d %H:%M:%S"))
                if int(online_count) != 0:
                    new_info.save()
                    print '===============success=====platform_list============='
            else:
                print 'exist--======e=-=-=-e=e-e=-=-=-=-'
        Unixtime = time.mktime(time.strptime(changed_time.strftime('%Y-%m-%d %H:%M:%S'), '%Y-%m-%d %H:%M:%S'))
        b_date = Unixtime


get_back_data()




















